package com.ruoyi.common.utils.poi.word.render.business;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.ruoyi.common.constant.BusinessConstant;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.mapper.*;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.word.render.WordRenderHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.ruoyi.common.utils.file.FileUtils.saveBase64Img;

/**
 * 专家服务绩效评定表（综合性活动使用）帮扶组两人以上签字、督查处负责人签字、
 */
@Component
public class T0102Handler extends WordRenderHandler {

    /*private ActivityScore activityScore;
    private Activity activity;
    private Expert expert;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityScoreMapper activityScoreMapper;
    @Autowired
    private ExpertMapper expertMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ActivityGroupMapper activityGroupMapper;



    @Override
    public void makeData() throws IOException {
        super.makeData();
        List<ActivityGroup> groupList = activityGroupMapper.findByActivityId(activity.getId());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        groupList = groupList.stream().filter(r -> r.getLeaderId() != null && user.getUserId().equals(r.getLeaderId())).collect(Collectors.toList());
        String activityLocation = activity.getLocation();
        if("综合性活动".equals(activity.getContentScopeCategory()) && groupList.size() > 0){
            activityLocation = groupList.stream().map(ActivityGroup::getLocation).collect(Collectors.joining("、"));
        }
        if(StrUtil.isBlank(activityLocation)){
            activityLocation = "";
        }
        this.add("activityName", activity.getActivityName());
        this.add("activityDescription", activity.getDescription());

        this.add("activityLocation", activityLocation);
        this.add("activityDate", DateUtil.format(activity.getActivityStartDate(), "yyyy年MM月dd日") + "至" + DateUtil.format(activity.getActivityEndDate(), "yyyy年MM月dd日"));

        this.add("expertName", expert.getName());
        this.add("workUnit", expert.getWorkUnit());
        this.add("currentSpecialty", expert.getCurrentSpecialty());
        this.add("expertTitle", expert.getTitle());
        this.add("expertGroups", expert.getExpertGroups());
        this.add("expertPhone", expert.getContactPhone());
        this.add("ethicsScore", activityScore.getEthicsScore());
        this.add("capabilityScore", activityScore.getCapabilityScore());
        this.add("performanceScore", activityScore.getPerformanceScore());
        this.add("integrityScore", activityScore.getIntegrityScore());
        this.add("hasIllegalBehavior", activityScore.getHasIllegalBehavior().intValue() == 1 ? "□无  √有" : "√无  □有");
        this.add("illegalBehaviorDescription", activityScore.getIllegalBehaviorDescription());


        //服务对象（部门、企业单位）评定分值（60%）
        List<ActivityScore> activityScoreList = activityScoreMapper.findByActivityIdAndExpertId(activityScore.getActivityId(), activityScore.getExpertId());
        List<ActivityScore> serviceActivityScoreList = activityScoreList.stream()
                .filter(r -> "3".equals(r.getScoreSheetType()) && "1".equals(r.getScoringStatus())).collect(Collectors.toList());
        Assert.isFalse(serviceActivityScoreList.isEmpty(), "服务对象（部门、企业单位）未对专家[" + expert.getName() + "]打分");

        BigDecimal serviceTargetScore = BigDecimal.ZERO;
        for (ActivityScore serviceActivityScore : serviceActivityScoreList) {
            serviceTargetScore = NumberUtil.add(serviceTargetScore, serviceActivityScore.getTotalScore());
        }
        // 指定精度和舍入模式以避免ArithmeticException
        serviceTargetScore = serviceTargetScore.divide(new BigDecimal(serviceActivityScoreList.size()), 4, RoundingMode.HALF_UP);
        serviceTargetScore = serviceTargetScore.multiply(new BigDecimal("0.6")).setScale(2, RoundingMode.HALF_UP);
        this.add("serviceTargetScore", serviceTargetScore);

        ActivityScore deptActivityScoreObj = activityScoreList.stream()
                .filter(r -> !"3".equals(r.getScoreSheetType()) && "1".equals(r.getScoringStatus())).findAny().orElse(null);
        Assert.isFalse(deptActivityScoreObj == null, "部门打分不存在");
        // 使用字符串构造BigDecimal避免浮点数精度问题
        BigDecimal deptActivityScore = deptActivityScoreObj.getTotalScore().multiply(new BigDecimal("0.4")).setScale(2, RoundingMode.HALF_UP);
        this.add("deptActivityScore", deptActivityScore);

        BigDecimal totalScore = serviceTargetScore.add(deptActivityScore);
        this.add("totalScore", totalScore);
        this.add("scoreLevel", getScoreLevel(totalScore));

        SysDept dept = sysDeptMapper.selectById(activity.getDeptId());
        this.add("dept", dept.getDeptName());

        addBusinessSignImg(BusinessConstant.SignRoleFlag.inputSigner,activityScore);
        addBusinessSignImg(BusinessConstant.SignRoleFlag.groupLeader,activityScore);
        addBusinessSignImg(BusinessConstant.SignRoleFlag.checkDeptLeader,activityScore);

    }

    *//**
     * 获取评价等级
     * 绩效评定实行百分制，分五个等次，即：90分（含）至100分为一等次，80分（含）至90分为二等次，70分（含）至80分为三等次，60分（含）至70分为四等次，60分以下为五等次。
     *//*
    public String getScoreLevel(BigDecimal score) {
        if (score.compareTo(new BigDecimal(90)) >= 0) {
            return "优秀";
        } else if (score.compareTo(new BigDecimal(80)) >= 0) {
            return "良好";
        } else if (score.compareTo(new BigDecimal(70)) >= 0) {
            return "一般";
        } else if (score.compareTo(new BigDecimal(60)) >= 0) {
            return "差";
        } else {
            return "极差";
        }
    }

    @Override
    public void renderAfter() throws Exception {


    }

    @Override
    public void setObject(Object object) {
        super.setObject(object);
        activityScore = (ActivityScore) object;
        activity = activityMapper.selectById(activityScore.getActivityId());
        expert = expertMapper.selectById(activityScore.getExpertId());
    }*/
}
